package com.dalei.vds;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.dalei.vds.soap.TimeSoapImpl;
import com.dalei.vds.util.Clock;
import com.dalei.vdserver.shared.soap.TimeSoap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TimeSettingsActivity extends Activity {

	private static final int MESSAGE_SHOW_DIALOG = 0x00000000;

	private static final int MESSAGE_DISMISS_DIALOG = 0x00000001;

	private static final int MESSAGE_SHOW_TOAST = 0x00000002;

	private static final int MESSAGE_REFRESH = 0x00000003;

	private static final long REFRESH_CYCLE = 500;

	private TextView timeText;

	private Button setBtn;

	private ProgressDialog dialog;

	private Handler handler;

	private boolean autoFresh = true;

	private boolean keepWaiting = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timesettings);

		handler = new IncomingHandler(this);

		timeText = (TextView) findViewById(R.id.timesettings_time);

		setBtn = (Button) findViewById(R.id.timesettings_set);
		setBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				ComponentName component = new ComponentName(
						"com.android.settings",
						"com.android.settings.DateTimeSettings");
				intent.setComponent(component);
				intent.setAction("android.settings.VIEW");
				startActivity(intent);
			}
		});

		Thread thread = new TimeThread(this);
		thread.start();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		autoFresh = false;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		keepWaiting = true;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		keepWaiting = false;
	}

	private static class IncomingHandler extends Handler {

		private WeakReference<TimeSettingsActivity> reference;

		public IncomingHandler(TimeSettingsActivity activity) {
			// TODO Auto-generated constructor stub
			reference = new WeakReference<TimeSettingsActivity>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			// super.handleMessage(msg);
			TimeSettingsActivity activity = reference.get();
			switch (msg.what) {
			case MESSAGE_SHOW_DIALOG:
				activity.dialog = ProgressDialog.show(activity, null,
						activity.getString(R.string.waitting));
				break;

			case MESSAGE_DISMISS_DIALOG:
				activity.dialog.dismiss();
				break;

			case MESSAGE_SHOW_TOAST:
				Toast.makeText(activity, activity.getString(msg.arg1),
						Toast.LENGTH_SHORT).show();
				break;

			case MESSAGE_REFRESH:
				Date date = (Date) msg.obj;
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy/MM/dd HH:mm:ss", Locale.CHINA);
				String dateString = simpleDateFormat.format(date);
				activity.timeText.setText(dateString);
				break;

			default:
				break;
			}
		}
	}

	private class TimeThread extends Thread {

		private Context context;

		public TimeThread(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			handler.sendEmptyMessage(MESSAGE_SHOW_DIALOG);
			TimeSoap timeSoap = new TimeSoapImpl(context);
			long time = timeSoap.getServerTime();
			handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
			if (time > 0) {
				Clock clock = new Clock(time);
				while (autoFresh) {
					Message msg = handler.obtainMessage();
					msg.what = MESSAGE_REFRESH;
					msg.obj = clock.getTime();
					handler.sendMessage(msg);
					try {
						Thread.sleep(REFRESH_CYCLE);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					while (keepWaiting) {
						
					}
				}
			} else {
				Message msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.get_server_time_failed;
				handler.sendMessage(msg);
			}
		}
	}
}
